<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

aos_header( 'Letters' );

global $page, $max_page;

$page = getPageNumber();
$query = "SELECT threaded_letters FROM users WHERE user_id = " . $_SESSION[ 'user_id' ];
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
if( $row[ 'threaded_letters' ] == 1 ) {
	$threaded = true;
	$per_page = 10;
}
else {
	$threaded = false;
	$per_page = 25;
}
$query = "SELECT faction_id " .
		"FROM faction_member " .
		"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND invited = 0 AND requested = 0";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
if( isset( $row[ 'faction_id' ] ) ) {
	$faction_id_tmp = $row[ 'faction_id' ];
}
else {
	$faction_id_tmp = -1;
}
if( isset( $_GET ) && isset( $_GET[ 'trashed' ] ) ) {
	$trashed = true;
	$hide = 1;
	$title = translate( 'Trashed letters' ) . ": ";
}
else {
	$trashed = false;
	$hide = 0;
	$title = translate( 'Letters received' ) . ": ";
}

if( $threaded ) {
	$sender = "";
	$sender2 = "";
	if( !$trashed ) {
		$sender = " OR sender = " . $_SESSION[ 'ruler_id' ];
		$sender2 = " OR messages2.sender = " . $_SESSION[ 'ruler_id' ];
	}
	$query2 = "SELECT count(*) FROM messages AS messages1 " .
				"WHERE ( message_id IN " .
					"( SELECT message_id " .
					"FROM message_ruler " .
					"WHERE hide = $hide AND ruler_id = " . $_SESSION[ 'ruler_id' ] . " )$sender ) " . 
				"AND ( messages1.parent = -1 OR " .
					"( SELECT count(*) " .
					"FROM message_ruler AS message_ruler2 " .
					"JOIN messages AS messages2 ON messages2.message_id = message_ruler2.message_id " .
					"WHERE messages2.message_id = messages1.parent AND message_ruler2.hide = $hide AND ( message_ruler2.ruler_id = " . $_SESSION[ 'ruler_id' ] . "$sender2 ) ) = 0 )";
	//print( "query2: $query2 <br />" );
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$total = $row2[ 'count(*)' ];
	$max_page = ceil( $total / $per_page );
	if( $max_page > 1 ) {
		$title .= " " . translate( 'showing threads' ) . " " . number_format( ( $page - 1 ) * $per_page ) . " " . translate( 'to' ) . " " . number_format( $page * $per_page ) . " " . translate( 'out of' ) . " " . number_format( $total );
	}
	else {
		$title .= " " . translate( 'showing' ) . " " . number_format( $total ) . " " . translate( 'threads' );
	} 
	$query = "SELECT DISTINCT messages1.* FROM messages AS messages1 " .
				"WHERE ( message_id IN " .
					"( SELECT message_id " .
					"FROM message_ruler " .
					"WHERE hide = $hide AND ruler_id = " . $_SESSION[ 'ruler_id' ] . " )$sender ) " . 
				"AND ( messages1.parent = -1 OR " .
					"( SELECT count(*) " .
					"FROM message_ruler AS message_ruler2 " .
					"JOIN messages AS messages2 ON messages2.message_id = message_ruler2.message_id " .
					"WHERE messages2.message_id = messages1.parent AND message_ruler2.hide = $hide AND ( message_ruler2.ruler_id = " . $_SESSION[ 'ruler_id' ] . "$sender2 ) ) = 0 ) " .
				"ORDER BY messages1.turn DESC, messages1.timestamp DESC " .
				"LIMIT " . ( ( $page - 1 ) * $per_page ) . ', '. $per_page;
	//print( "query: $query <br />" );
}
else {
	$threaded = false;
	$query2 = "SELECT count(*) FROM messages " .
			"JOIN message_ruler ON message_ruler.message_id = messages.message_id " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND hide = $hide ";
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$total = $row2[ 'count(*)' ];
	$max_page = ceil( $total / $per_page );
	if( $max_page > 1 ) {
		$title .= " " . translate( 'showing letters' ) . " " . number_format( ( $page - 1 ) * $per_page ) . " " . translate( 'to' ) . " " . number_format( $page * $per_page ) . " " . translate( 'out of' ) . " " . number_format( $total );
	}
	else {
		$title .= " " . translate( 'showing' ) . " " . number_format( $total ) . " " . translate( 'letters' ) . "";
	} 
	
	$query = "SELECT DISTINCT messages.* FROM messages " .
			"JOIN message_ruler ON message_ruler.message_id = messages.message_id " .
			"WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND hide = $hide " .
			"ORDER BY turn DESC, timestamp DESC " .
			"LIMIT " . ( ( $page - 1 ) * $per_page ) . ', '. $per_page;
}
//print( "Query: $query <br />" );
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );
print "<div class=\"blok\">";

if( $trashed ) {
	printDiplomacyTab( 'Trashed letters' );
}
else {
	printDiplomacyTab( 'Letters received' ); 
}
print "</div><br/>\r\n";
if( $num > 0 ) {
	print "<form method=\"post\" action=\"letter_action.php\">\r\n";
	print "<table><th colspan=\"4\">$title</th>\r\n";
	print "<tr><td>" . translate( 'Select' ) . "</td><td>" . translate( 'Subject' ) . "</td><td>" . translate( 'From' ) . "</td><td>" . translate( 'Date' ) . "</td></tr>\r\n";
	$i = 0;
	while ( $i < $num ) {
	    if( $i % 2 == 0 ) {
	        print "<tr id=\"even\">";
	    }
	    else {
	        print "<tr>";
	    }
	    $query2 = "SELECT name FROM ruler WHERE ruler_id = " . mysql_result( $result, $i, "sender" );
	    $result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
		$row = mysql_fetch_array( $result2, MYSQL_ASSOC );
		
	    $query3 = "SELECT faction_id FROM faction_member WHERE ruler_id = " . mysql_result( $result, $i, "sender" ) . " AND invited = 0 AND requested = 0";
	    $result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
		$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
		if( $_SESSION[ 'ruler_id' ] == mysql_result( $result, $i, "sender" ) ) {
			$strong = "<strong class=\"green\">";
		}
		else if( !isset( $row3[ 'faction_id' ] ) || $faction_id_tmp != $row3[ 'faction_id' ] ) {
			$strong = "<strong class=\"red\">";
		}
		else {
			$strong = "<strong class=\"blue\">";
		}
		
		$unread_thread = false;
		$seen = true;
		if( $threaded ) {
			$count = getThreadCount( mysql_result( $result, $i, "message_id" ) );
			$unread_thread = isUnreadThread( mysql_result( $result, $i, "message_id" ) );
			//print( "Result: " . $unread_thread . "<br />" );
			if( $count > 1 ) {
				$count = " (" . $count . ")";
			}
			else {
				$count = "";
			}
			$goto = getOldestUnread( mysql_result( $result, $i, "message_id" ) );
			if( is_array( $goto ) ) {
				$goto = $goto[1];
			}
		}
		else {
			$goto = mysql_result( $result, $i, "message_id" );
			$count = "";
		    $query4 = "SELECT seen FROM message_ruler WHERE ruler_id = " . $_SESSION[ 'ruler_id' ] . " AND message_id = " . mysql_result( $result, $i, "message_id" );
		    $result4 = mysql_query( $query4 ) or die( "Query failed : " . mysql_error() );
			$row4 = mysql_fetch_array( $result4, MYSQL_ASSOC );
		    if( $row4[ 'seen' ] == 0 ) {
		    	$seen = false;
		    }
		    else {
		    	$seen = true;
		    }
		    
		}
	
		if( !$seen || $unread_thread ) {
			$b = "<strong>";
			$close_b = "</strong>";
		}
		else {
			$b = "";
			$close_b = "";
		}
		
		if( $threaded && mysql_result( $result, $i, "sender" ) == $_SESSION[ 'ruler_id' ] && $count == '' ) {
	    	print(  "<td>&nbsp;</td>" );
		}
		else {
	    	print(  "<td><input id=\"checkbox$i\" type=\"checkbox\" name=\"$i\" /></td>" );
		}
		print( "<td><a href=\"letter.php?id=" .  $goto . "#focus\">" . $b . stripslashes( mysql_result( $result, $i, "subject" ) ) . $count . $close_b . "</a></td><td><a href=\"../overview/ruler.php?name=" . $row[ 'name' ] . "\">$strong" . $row[ 'name' ] . "</strong></a></td><td>$b" .  mysql_result( $result, $i, "date" ) );
	    print "$close_b</td></tr>\r\n";
	
	    $i ++;
	}
	print "</table>";
    print "<a href=\"javascript:void();\" onclick=\"setSelect(true);\">select all</a> ";
    print "<a href=\"javascript:void();\" onclick=\"setSelect(false);\">select none</a> ";
	print "<input type=\"hidden\" value=\"$page\" name=\"page\" /> ";
	print "<input type=\"submit\" value=\"" . translate( 'mark read' ) . "\" name=\"read\" /> ";
	print "<input type=\"submit\" value=\"" . translate( 'mark unread' ) . "\" name=\"unread\" /> ";
	if( $trashed ) {
		print( "<input type=\"hidden\" name=\"trashed\" value=\"untrash\" />\r\n" );
		print( "<input type=\"submit\" value=\"" . translate( 'move back' ) . "\" name=\"hide\" /></form>" );
	}
	else {
		print( "<input type=\"submit\" value=\"" . translate( 'trash' ) . "\" name=\"hide\" /></form>" );
	}
	
	if( $max_page > 1 ) {
		print( "<br /><br />\r\n" );
		if( $trashed ) {
			print( "<div class=\"previous-link\">" . getPreviousPageLink( translate( 'Previous' ), 'letters.php?view=trashed', true ) . "</div>" );
			print( "<div class=\"next-link\">" . getNextPageLink( translate( 'Next' ) , 'letters.php?view=trashed', true ) . "</div>" );
		}
		else {
			print( "<div class=\"previous-link\">" . getPreviousPageLink( translate( 'Previous' ), 'letters.php' ) . "</div>" );
			print( "<div class=\"next-link\">" . getNextPageLink( translate( 'Next' ), 'letters.php' ) . "</div>" );
		}
	}
}
else if( $page == 1 ) {
	print( "<div class=\"blok\">\r\n" );
	if( $trashed ) {
		print( translate( 'You have no trashed letters.' ) . "<br />\r\n" );
	}
	else {
		print( translate( 'You have no letters.' ) . "<br />\r\n" );
	}
	print( "</div>\r\n" );    
}
else {
	print( "<div class=\"blok\">\r\n" );
	print( translate( 'No letters on this page.' ) . "<br />\r\n" );
	print( "</div>\r\n" );    
}
?>
				<script language="JavaScript" type="text/javascript">
					function setSelect( toggle ) {
						var still = true;
						var i = 0;
						while ( still ) {
							var tmp = document.getElementById( "checkbox" + i );
							if( !tmp ) {
								still = false;
							}
							else {
								tmp.checked = toggle;
							}
							i ++;
						}
					}
				</script>
<?php
aos_footer();
?>
